home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / mc-3.2 / mc-3 / mc-3.2.1 / vfs / undelfs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-17  |  16.0 KB  |  750 lines

  1. /* UnDel File System: Midnight Commander file system.
  2.  
  3.    This file system is intended to be used together with the
  4.    ext2fs library to recover files from ext2fs file systems.
  5.  
  6.    Parts of this program were taken from the lsdel.c and dump.c files
  7.    written by Ted Ts'o (tytso@lurch.mit.edu) for the ext2fs package.
  8.    
  9.    Copyright (C) 1995 the Free Software Foundation
  10.    Written by: 1995 Miguel de Icaza.
  11.    
  12.    This program is free software; you can redistribute it and/or modify
  13.    it under the terms of the GNU General Public License as published by
  14.    the Free Software Foundation; either version 2 of the License, or
  15.    (at your option) any later version.
  16.    
  17.    This program is distributed in the hope that it will be useful,
  18.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.    GNU General Public License for more details.
  21.  
  22.    You should have received a copy of the GNU General Public License
  23.    along with this program; if not, write to the Free Software
  24.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  25.  
  26. /* Assumptions:
  27.  *
  28.  * 1. We don't handle directories (thus undelfs_getpath is easy to write).
  29.  * 2. Files are on the local file system (we do not support vfs files
  30.  *    because we would have to provide an io_manager for the ext2fs tools,
  31.  *    and I don't think it would be too useful to undelete files 
  32.  */
  33.  
  34. #include <config.h>
  35. #include <errno.h>
  36. #include "../src/fs.h"
  37. #include "../src/mad.h"
  38.  
  39. #include "../src/mem.h"
  40. #include "vfs.h"
  41.  
  42. #include <stdio.h>
  43. #include <malloc.h>
  44. #include <fcntl.h>
  45. #include <linux/ext2_fs.h>
  46. #include <ext2fs/ext2fs.h>
  47.  
  48. struct deleted_info {
  49.     ino_t   ino;
  50.     unsigned short mode;
  51.     unsigned short uid;
  52.     unsigned short gid;
  53.     unsigned long size;
  54.     time_t  dtime;
  55.     int     num_blocks;
  56.     int     free_blocks;
  57. };
  58.  
  59. struct lsdel_struct {
  60.     ino_t                   inode;
  61.     int                     num_blocks;
  62.     int                     free_blocks;
  63.     int                     bad_blocks;
  64. };
  65.  
  66. /* We only allow one opened ext2fs */
  67. static char *ext2_fname = "";
  68. static ext2_filsys fs = NULL;
  69. static struct lsdel_struct     lsd;
  70. static struct deleted_info     *delarray;
  71. static int num_delarray, max_delarray;
  72. static char *block_buf;
  73. static char *undelfserr = " undelfs: error ";
  74. static int readdir_ptr;
  75.  
  76. /* To generate the . and .. entries use -2 */
  77. #define READDIR_PTR_INIT 0
  78.  
  79. static void
  80. undelfs_shutdown ()
  81. {
  82.     /* should shutdown the ext2 toolkit */
  83. }
  84.  
  85. static void
  86. undelfs_get_path (char *dirname, char **ext2_fname, char **file)
  87. {
  88.     char *p;
  89.  
  90.     *ext2_fname = 0;
  91.     
  92.     if (strncmp (dirname, "undel:", 6))
  93.     return;
  94.     else
  95.     dirname += 6;
  96.  
  97.     /* Since we don't allow subdirectories, it's easy to get a filename,
  98.      * just scan backwards for a slash
  99.      */
  100.     if (*dirname == 0)
  101.     return;
  102.     
  103.     p = dirname + strlen (dirname);
  104.     if (p - dirname > 2 && *(p-1) == '/' && *(p-2) == '.')
  105.     *(p = p-2) = 0;
  106.     
  107.     while (p > dirname){
  108.     if (*p == '/'){
  109.         *file = strdup (p+1);
  110.         *p = 0;
  111.         *ext2_fname = strdup (dirname);
  112.         *p = '/';
  113.         return;
  114.     }
  115.     p--;
  116.     }
  117. }
  118.  
  119. static int
  120. lsdel_proc(ext2_filsys fs, blk_t *block_nr, int blockcnt, void *private)
  121. {
  122.     struct lsdel_struct *lsd = (struct lsdel_struct *) private;
  123.     
  124.     lsd->num_blocks++;
  125.     
  126.     if (*block_nr < fs->super->s_first_data_block ||
  127.     *block_nr >= fs->super->s_blocks_count) {
  128.     lsd->bad_blocks++;
  129.     return BLOCK_ABORT;
  130.     }
  131.     
  132.     if (!ext2fs_test_block_bitmap(fs->block_map,*block_nr))
  133.     lsd->free_blocks++;
  134.     
  135.     return 0;
  136. }
  137.  
  138. /* We don't use xmalloc, since we want to recover and not abort the program
  139.  * if we don't have enough memory
  140.  */
  141. static int
  142. undelfs_loaddel ()
  143. {
  144.     int retval, count;
  145.     ino_t ino;
  146.     struct ext2_inode inode;
  147.     ext2_inode_scan   scan;
  148.     
  149.     max_delarray = 100;
  150.     num_delarray = 0;
  151.     delarray = malloc(max_delarray * sizeof(struct deleted_info));
  152.     if (!delarray) {
  153.     message (1, undelfserr, " not enough memory ");
  154.     return 0;
  155.     }
  156.     block_buf = malloc(fs->blocksize * 3);
  157.     if (!block_buf) {
  158.     message (1, undelfserr, " while allocating block buffer ");
  159.     goto free_delarray;
  160.     }
  161.     if ((retval = ext2fs_open_inode_scan(fs, 0, &scan))){
  162.     message (1, undelfserr, " open_inode_scan: %d ", retval);
  163.     goto free_block_buf;
  164.     }
  165.     if ((retval = ext2fs_get_next_inode(scan, &ino, &inode))){
  166.     message (1, undelfserr, " while starting inode scan %d ", retval);
  167.     goto free_block_buf;
  168.     }
  169.  
  170.     count = 0;
  171.     while (ino) {
  172.     if ((count++ % 1024) == 0) 
  173.         print_vfs_message ("undelfs: loading deleted files information %d inodes", count);
  174.     if (inode.i_dtime == 0)
  175.         goto next;
  176.  
  177.     if (S_ISDIR(inode.i_mode))
  178.         goto next;
  179.  
  180.     lsd.inode = ino;
  181.     lsd.num_blocks = 0;
  182.     lsd.free_blocks = 0;
  183.     lsd.bad_blocks = 0;
  184.  
  185.     retval = ext2fs_block_iterate(fs, ino, 0, block_buf,
  186.                       lsdel_proc, &lsd);
  187.     if (retval) {
  188.         message (1, undelfserr, " while calling ext2_block_iterate %d ", retval);
  189.         goto next;
  190.     }
  191.     if (lsd.free_blocks && !lsd.bad_blocks) {
  192.         if (num_delarray >= max_delarray) {
  193.         max_delarray += 50;
  194.         delarray = realloc(delarray,
  195.                    max_delarray * sizeof(struct deleted_info));
  196.         if (!delarray) {
  197.             message (1, undelfserr, " no more memory while reallocating array ");
  198.             goto free_block_buf;
  199.         }
  200.         }
  201.         
  202.         delarray[num_delarray].ino = ino;
  203.         delarray[num_delarray].mode = inode.i_mode;
  204.         delarray[num_delarray].uid = inode.i_uid;
  205.         delarray[num_delarray].gid = inode.i_gid;
  206.         delarray[num_delarray].size = inode.i_size;
  207.         delarray[num_delarray].dtime = inode.i_dtime;
  208.         delarray[num_delarray].num_blocks = lsd.num_blocks;
  209.         delarray[num_delarray].free_blocks = lsd.free_blocks;
  210.         num_delarray++;
  211.     }
  212.  
  213.     next:
  214.     retval = ext2fs_get_next_inode(scan, &ino, &inode);
  215.     if (retval) {
  216.         message(1, undelfserr, " while doing inode scan %d ", retval);
  217.         goto error_out;
  218.     }
  219.     }
  220.     readdir_ptr = READDIR_PTR_INIT;
  221.     return 1;
  222.     
  223. error_out:    
  224. free_block_buf:
  225.     free (block_buf);
  226.     block_buf = 0;
  227. free_delarray:
  228.     free (delarray);
  229.     delarray = 0;
  230.     return 0;
  231. }
  232.  
  233. void com_err (const char *str, long err_code, const char *s2, ...)
  234. {
  235.     message (1, " Ext2lib error ",
  236.          " %s (%s: %d) ", s2, str, err_code);
  237. }
  238.  
  239. static void *
  240. undelfs_opendir (char *dirname)
  241. {
  242.     char *file, *f;
  243.     
  244.     undelfs_get_path (dirname, &file, &f);
  245.     if (!file)
  246.     return 0;
  247.  
  248.     /* We don't use the file name */
  249.     free (f);
  250.     
  251.     if (strcmp (ext2_fname, file)){
  252.     undelfs_shutdown ();
  253.     ext2_fname = file;
  254.     } else {
  255.     /* To avoid expensive re-scannings */
  256.     readdir_ptr = READDIR_PTR_INIT;
  257.     free (file);
  258.     return fs;
  259.     }
  260.  
  261.     if (ext2fs_open (ext2_fname, 0, 0, 0, unix_io_manager, &fs)){
  262.     message (1, undelfserr, " Could not open file %s ", ext2_fname);
  263.     return 0;
  264.     }
  265.     print_vfs_message ("undelfs: reading inode bitmap...");
  266.     if (ext2fs_read_inode_bitmap (fs)){
  267.     message (1, undelfserr,
  268.          " Could not load inode bitmap from: \n %s \n", ext2_fname);
  269.     goto quit_opendir;
  270.     }
  271.     print_vfs_message ("undelfs: reading block bitmap...");
  272.     if (ext2fs_read_block_bitmap (fs)){
  273.     message (1, undelfserr,
  274.          " Could not load block bitmap from: \n %s \n", ext2_fname);
  275.     goto quit_opendir;
  276.     }
  277.     /* Now load the deleted information */
  278.     if (!undelfs_loaddel ())
  279.     goto quit_opendir;
  280.     print_vfs_message ("undelfs: done.");
  281.     return fs;
  282. quit_opendir:
  283.     print_vfs_message ("undelfs: failure");
  284.     ext2fs_close (fs);
  285.     fs = NULL;
  286.     return 0;
  287. }
  288.  
  289. /* Explanation:
  290.  * On some operating systems (Slowaris 2 for example)
  291.  * the d_name member is just a char long (Nice trick that break everything,
  292.  * so we need to set up some space for the filename.
  293.  */
  294. static struct {
  295.     struct dirent dent;
  296. #ifdef NEED_EXTRA_DIRENT_BUFFER
  297.     char extra_buffer [MC_MAXPATHLEN];
  298. #endif
  299. } undelfs_readdir_data;
  300.  
  301. static void *
  302. undelfs_readdir (void *vfs_info)
  303. {
  304.     char *dirent_dest;
  305.     
  306.     if (vfs_info != fs){
  307.     message (1, " delfs: internal error ",
  308.          " vfs_info is not fs! ");
  309.     return NULL;
  310.     }
  311.     if (readdir_ptr == num_delarray)
  312.     return NULL;
  313.     dirent_dest = &(undelfs_readdir_data.dent.d_name [0]);
  314.     if (readdir_ptr < 0)
  315.     sprintf (dirent_dest, "%s", readdir_ptr == -2 ? "." : "..");
  316.     else
  317.     sprintf (dirent_dest, "%ld:%ld",
  318.          delarray [readdir_ptr].ino,
  319.          delarray [readdir_ptr].num_blocks);
  320.     readdir_ptr++;
  321.     
  322. #ifndef DIRENT_LENGTH_COMPUTED
  323.     undelfs_readdir_data.dent.d_namlen = strlen (undelfs_readdir_data.dent.d_name);
  324. #endif
  325.     return &undelfs_readdir_data;
  326. }
  327.  
  328. static int
  329. undelfs_closedir (void *vfs_info)
  330. {
  331.     return 0;
  332. }
  333.  
  334. typedef struct {
  335.     int  f_index;            /* file index into delarray */
  336.     char *buf;
  337.     int  error_code;        /*  */
  338.     int  pos;            /* file position */
  339.     int  current;        /* used to determine current position in itereate */
  340.     int  finished;
  341.     long inode;
  342.     int  bytes_read;
  343.     long size;
  344.     
  345.     /* Used by undelfs_read: */
  346.     char *dest_buffer;        /* destination buffer */
  347.     int  count;            /* bytes to read */
  348. } undelfs_file;
  349.  
  350. /* We do not support lseek */
  351. static void *
  352. undelfs_open (char *fname, int flags, int mode)
  353. {
  354.     char *file, *f;
  355.     int  inode, i;
  356.     undelfs_file *p;
  357.     
  358.     /* Only allow reads on this file system */
  359.     undelfs_get_path (fname, &file, &f);
  360.     if (!file)
  361.     return 0;
  362.     
  363.     if (strcmp (ext2_fname, file)){
  364.     message (1, undelfserr, " You have to chdir to extract files first ");
  365.     free (file);
  366.     free (f);
  367.     return 0;
  368.     }
  369.     inode = atol (f);
  370.  
  371.     /* Search the file into delarray */
  372.     for (i = 0; i < num_delarray; i++){
  373.     if (inode != delarray [i].ino)
  374.         continue;
  375.  
  376.     /* Found: setup all the structures needed by read */
  377.     p = (void *) malloc (sizeof (undelfs_file));
  378.     if (!p){
  379.         free (file);
  380.         free (f);
  381.         return 0;
  382.     }
  383.     p->buf = malloc(fs->blocksize);
  384.     if (!p->buf){
  385.         free (p);
  386.         free (file);
  387.         free (f);
  388.         return 0;
  389.     }
  390.     p->inode = inode;
  391.     p->finished = 0;
  392.     p->f_index = i;
  393.     p->error_code = 0;
  394.     p->pos = 0;
  395.     p->size = delarray [i].size;
  396.     }
  397.     return p;
  398. }
  399.  
  400. static     int
  401. undelfs_close (void *vfs_info)
  402. {
  403.     return 0;
  404. }
  405.  
  406. static int
  407. dump_read(ext2_filsys fs, blk_t *blocknr, int blockcnt, void *private)
  408. {
  409.     ssize_t nbytes;
  410.     int     copy_count;
  411.     undelfs_file *p = (undelfs_file *) private;
  412.     
  413.     if (blockcnt < 0)
  414.     return 0;
  415.     
  416.     if (*blocknr) {
  417.     p->error_code = io_channel_read_blk(fs->io, *blocknr,
  418.                        1, p->buf);
  419.     if (p->error_code)
  420.         return BLOCK_ABORT;
  421.     } else
  422.     memset(p->buf, 0, fs->blocksize);
  423.  
  424.     if (p->pos + p->count < p->current){
  425.     p->finished = 1;
  426.     return BLOCK_ABORT;
  427.     }
  428.     if (p->pos > p->current + fs->blocksize){
  429.     p->current += fs->blocksize;
  430.     return 0;        /* we have not arrived yet */
  431.     }
  432.  
  433.     /* Now, we know we have to extract some data */
  434.     if (p->pos >= p->current){
  435.  
  436.     /* First case: starting pointer inside this block */
  437.     if (p->pos + p->count <= p->current + fs->blocksize){
  438.         /* Fully contained */
  439.         copy_count = p->count;
  440.         p->finished = p->count;
  441.     } else {
  442.         /* Still some more data */
  443.         copy_count = fs->blocksize-(p->pos-p->current);
  444.     }
  445.     memcpy (p->dest_buffer, p->buf + (p->pos-p->current), copy_count);
  446.     } else {
  447.     /* Second case: we already have passed p->pos */
  448.     if (p->pos+p->count < p->current+fs->blocksize){
  449.         copy_count = (p->pos + p->count) - p->current;
  450.         p->finished = p->count;
  451.     } else {
  452.         copy_count = fs->blocksize;
  453.     }
  454.     memcpy (p->dest_buffer, p->buf, copy_count);
  455.     }
  456.     p->dest_buffer += copy_count;
  457.     p->current += fs->blocksize;
  458.     if (p->finished){
  459.     return BLOCK_ABORT;
  460.     }
  461.     return 0;
  462. }
  463.  
  464. static int
  465. undelfs_read (void *vfs_info, char *buffer, int count)
  466. {
  467.     undelfs_file *p = vfs_info;
  468.     int retval;
  469.  
  470.     p->dest_buffer = buffer;
  471.     p->current     = 0;
  472.     p->finished    = 0;
  473.     p->count       = count;
  474.  
  475.     if (p->pos + p->count > p->size){
  476.     p->count = p->size - p->pos;
  477.     }
  478.     retval = ext2fs_block_iterate(fs, p->inode, 0, NULL,
  479.                   dump_read, p);
  480.     if (retval){
  481.     message (1, undelfserr, " while iterating over blocks ");
  482.     return -1;
  483.     }
  484.     if (p->error_code && !p->finished)
  485.     return 0;
  486.     p->pos = p->pos + (p->dest_buffer - buffer);
  487.     return p->dest_buffer - buffer;
  488. }
  489.  
  490. static int
  491. undelfs_write (void *vfs_info, char *buf, int count)
  492. {
  493.     /* No writes allowed */
  494.     return -1;
  495. }
  496.  
  497. static long
  498. undelfs_getindex (char *path)
  499. {
  500.     long inode = atol (path);
  501.     int i;
  502.  
  503.     for (i = 0; i < num_delarray; i++){
  504.     if (delarray [i].ino == inode)
  505.         return i;
  506.     }
  507.     return -1;
  508. }
  509.  
  510. static int
  511. do_stat (int inode_index, struct stat *buf)
  512. {
  513.     buf->st_dev   = 0;
  514.     buf->st_ino   = delarray [inode_index].ino;
  515.     buf->st_mode  = delarray [inode_index].mode;
  516.     buf->st_nlink = 1;
  517.     buf->st_uid   = delarray [inode_index].uid;
  518.     buf->st_gid   = delarray [inode_index].gid;
  519.     buf->st_size  = delarray [inode_index].size;
  520.     buf->st_atime = delarray [inode_index].dtime;
  521.     buf->st_ctime = delarray [inode_index].dtime;
  522.     buf->st_mtime = delarray [inode_index].dtime;
  523.     return 0;
  524. }
  525.  
  526. static int
  527. undelfs_lstat(char *path, struct stat *buf)
  528. {
  529.     int inode_index;
  530.     char *file, *f;
  531.     
  532.     undelfs_get_path (path, &file, &f);
  533.     if (!file)
  534.     return 0;
  535.     
  536.     if (strcmp (ext2_fname, file)){
  537.     message (1, undelfserr, " You have to chdir to extract files first ");
  538.     free (file);
  539.     free (f);
  540.     return 0;
  541.     }
  542.     inode_index = undelfs_getindex (f);
  543.  
  544.     if (inode_index == -1)
  545.     return -1;
  546.  
  547.     return do_stat (inode_index, buf);
  548. }
  549.  
  550. static int
  551. undelfs_stat(char *path, struct stat *buf)
  552. {
  553.     return undelfs_lstat (path, buf);
  554. }
  555.  
  556.  
  557. static int
  558. undelfs_fstat (void *vfs_info, struct stat *buf)
  559. {
  560.     undelfs_file *p = vfs_info;
  561.     
  562.     return do_stat (p->f_index, buf);
  563. }
  564.  
  565. static int
  566. undelfs_chmod(char *path, int mode)
  567. {
  568.     return -1;
  569. }
  570.  
  571. static int
  572. undelfs_chown(char *path, int owner, int group)
  573. {
  574.     return -1;
  575. }
  576.  
  577. static int
  578. undelfs_readlink(char *path, char *buf, int size)
  579. {
  580.     return -1;
  581. }
  582.  
  583. static int
  584. undelfs_symlink(char *n1, char *n2)
  585. {
  586.     return -1;
  587. }
  588.  
  589. static int
  590. undelfs_link(char *p1, char *p2)
  591. {
  592.     return -1;
  593. }
  594.  
  595. static int
  596. undelfs_unlink(char *path)
  597. {
  598.     return -1;
  599. }
  600.  
  601. static int
  602. undelfs_rename(char *p1, char *p2)
  603. {
  604.     return -1;
  605. }
  606.  
  607. static int
  608. undelfs_chdir(char *path)
  609. {
  610.     char *file, *f;
  611.     int fd;
  612.     
  613.     undelfs_get_path (path, &file, &f);
  614.     if (!file)
  615.     return -1;
  616.  
  617.     /* We may use access because ext2 file systems are local */
  618.     /* this could be fixed by making an ext2fs io manager to use */
  619.     /* our vfs, but that is left as an excercise for the reader */
  620.     if ((fd = open (file, O_RDONLY)) == -1){
  621.     message (1, undelfserr, " Could not open file: %s ", file);
  622.     return -1;
  623.     }
  624.     close (fd);
  625.     free (f);
  626.     free (file);
  627.     return 0;
  628. }
  629.  
  630. static int
  631. undelfs_ferrno(void)
  632. {
  633.     return -1;
  634. }
  635.  
  636. static int
  637. undelfs_lseek(void *vfs_info, off_t offset, int whence)
  638. {
  639.     return -1;
  640. }
  641.  
  642. static int
  643. undelfs_mknod(char *path, int mode, int dev)
  644. {
  645.     return -1;
  646. }
  647.  
  648. static vfsid
  649. undelfs_getid(char *path, struct vfs_stamping **parent)
  650. {
  651.     /* We run only on the local fs */
  652.     *parent = NULL;
  653.     return (vfsid) -1;
  654. }
  655.  
  656. static int
  657. undelfs_nothingisopen(vfsid id)
  658. {
  659.     return 0;
  660. }
  661.  
  662. static void
  663. undelfs_free(vfsid id)
  664. {
  665. }
  666.  
  667. static char *
  668. undelfs_getlocalcopy(char *filename)
  669. {
  670.     return 0;
  671. }
  672.  
  673. static void
  674. undelfs_ungetlocalcopy(char *filename, char *local, int has_changed)
  675. {
  676. }
  677.  
  678. static int
  679. undelfs_mkdir(char *path, mode_t mode)
  680. {
  681.     return -1;
  682. }
  683.  
  684. static int
  685. undelfs_rmdir(char *path)
  686. {
  687.     return -1;
  688. }
  689.  
  690.     
  691. #ifdef HAVE_MMAP
  692. static caddr_t
  693. undelfs_mmap(caddr_t addr, size_t len, int prot, int flags, void *vfs_info, off_t offset)
  694. {
  695.     return (caddr_t) -1;
  696. }
  697.  
  698. static int
  699. undelfs_munmap(caddr_t addr, size_t len, void *vfs_info)
  700. {
  701.     return -1;
  702. }
  703. #endif
  704.  
  705.  
  706. vfs undelfs_vfs_ops = {
  707.     undelfs_open,
  708.     undelfs_close,
  709.     undelfs_read,
  710.     undelfs_write,
  711.     
  712.     undelfs_opendir,
  713.     undelfs_readdir,
  714.     undelfs_closedir,
  715.  
  716.     undelfs_stat,
  717.     undelfs_lstat,
  718.     undelfs_fstat,
  719.  
  720.     undelfs_chmod,
  721.     undelfs_chown,
  722.  
  723.     undelfs_readlink,
  724.     undelfs_symlink,
  725.     undelfs_link,
  726.     undelfs_unlink,
  727.  
  728.     undelfs_rename,
  729.     undelfs_chdir,
  730.     undelfs_ferrno,
  731.     undelfs_lseek,
  732.     undelfs_mknod,
  733.     
  734.     undelfs_getid,
  735.     undelfs_nothingisopen,
  736.     undelfs_free,
  737.     
  738.     undelfs_getlocalcopy,
  739.     undelfs_ungetlocalcopy,
  740.  
  741.     undelfs_mkdir,
  742.     undelfs_rmdir,
  743.     NULL,
  744.     NULL
  745. #ifdef HAVE_MMAP
  746.     , undelfs_mmap,
  747.     undelfs_munmap
  748. #endif
  749. };
  750.